<?php

/**
* MimeType.class.php
*
* Class reposnable for file sending file to user with proper mime 
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/

require_once("framework/config.php");
require_once("framework/UserErrorHandler.class.php");
require_once("framework/ExceptionHandler.class.php");
require_once("MIME/Type/Extension.php");

// {{{ MimeType 

class MimeType extends MIME_Type_Extension
{
    // {{{ Class operations 

    // {{{ MimeType
    /**
    * C'tor
    */
    function MimeType()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','MIME_TYPE');
        $this->addMimeType( $options['type'] );
    }
    // }}}

    // {{{ get
    function get( $fileName )
    {
        $type = FALSE;
       
        $type = $this->getMIMEType(strtolower($fileName));

        if ( PEAR::isError($type) ) 
        {
            trigger_error("MimeType::get() ".$type->getMessage(), E_USER_ERROR); 
            return $type;
        }
        
        return $type;
    
    }
    // }}}

    // {{{ get
    private function addMimeType ( $string = "" )
    {
        if ( ! strlen ( $string )  ) 
        {
            return;
        }
        
        $tmpArray = explode ( "," , $string );
        
        if ( !( $tmpArray || count($tmpArray) ) )
        {
            return; 
        }
        
        foreach ( $tmpArray as $value ) 
        {
            $tmpExtensionToType = explode ("|", $value );
            if ( !( $tmpExtensionToType || count($tmpExtensionToType)  ) ) 
            {
                continue;
            }
    
            $ext = trim( $tmpExtensionToType[0]  );
            $type = trim( $tmpExtensionToType[1] );

            
            if ( isset( $this->extensionToType[ $ext ] ) )           
            {
                trigger_error("MimeType::addMimeType(): Existing type ".$this->extensionToType[ $ext ]." was overwritten", E_USER_NOTICE); 
            }

            $this->extensionToType[ $ext ]  = $type;
        }
    }
    // }}}

}

